package com.huifaapp.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;
import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.Red_PacketActivity;
import com.huifaapp.mall.activity.ShopYouhuiShareActivity;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.bean.BabyBean;
import com.huifaapp.mall.uitils.Contig;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mac on 2018/1/2.
 */

public class BabyAdapter  extends RecyclerView.Adapter <BabyAdapter.MyViewHolder>{

    private Context context;
    private List<BabyBean.DataBean.ListBean>goods;
    ArrayList<String> arrayList;
    private List<Boolean> isChecked;    //每个条目是否为选中
    private List<String> numbers;       //每个条目应显示的数字
    private List<Integer> checkedIndex;   //保存选中的下标
    private List<CheckBox> checkBox;      //保存选中的checkBox
    private int maxChecked = 9;       //最多选中几个
    private int checkedNumber = 0;    //选中的个数

    private int length = 1000000;  //条目的总长

    public BabyAdapter(Context context, List<BabyBean.DataBean.ListBean> goods) {
        this.context = context;
        this.goods = goods;

        arrayList = new ArrayList<String>();
        isChecked = new ArrayList<>();
        for (int i = 0; i < length; i++) {
            isChecked.add(false);
        }
        numbers = new ArrayList<>();
        for (int i = 0; i < length; i++) {
            numbers.add("");
        }
        checkedIndex = new ArrayList<>();
        checkBox = new ArrayList<>();

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.chioce_itemmmm,null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {

        final BabyBean.DataBean.ListBean dataBean  = goods.get(position);
        Glide.with(context).load(dataBean.getGoodsImg()).into(holder.baby_shopimg);
        holder.shopname.setText(dataBean.getGoodsName());
        holder.price_yj.setText("原价￥"+dataBean.getOriginPrice());
        holder.price_xj.setText(dataBean.getPrice()+"");
        holder.price_count.setText("已抢"+dataBean.getTotalSale()+"件");
        holder.couponAmount.setText("红包券￥"+dataBean.getCouponAmount());
//        holder.marketProfit.setText("赚 ¥ "+dataBean.getMarketProfit()+"");



        if(dataBean.getMarketProfit()==0.0||dataBean.getMarketProfit()==0.00){
            holder.tv_qianggou.setText("立即分享");
            holder.chioce_qianggou_no.setText("立即分享");
        }else{
            holder.tv_qianggou.setText("立即分享赚"+dataBean.getMarketProfit());
            holder.chioce_qianggou_no.setText("立即分享赚"+dataBean.getMarketProfit());
        }
        if(SPUtils.getInstance().getString(SPConstants.KEY_ISSTORE).equals("1")){
            holder.cb_checked_share_specila_coll.setVisibility(View.VISIBLE);
            holder.chioce_qianggoupt.setVisibility(View.GONE);
            holder.tv_qianggou.setVisibility(View.VISIBLE);
        }else{
            holder.cb_checked_share_specila_coll.setVisibility(View.GONE);
            holder.chioce_qianggoupt.setVisibility(View.VISIBLE);
            holder.tv_qianggou.setVisibility(View.GONE);
        }
        holder.tv_qianggou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, ShopYouhuiShareActivity.class);
                intent.putExtra("id",dataBean.getId());
                context.startActivity(intent);
                holder.chioce_qianggou_no.setVisibility(View.VISIBLE);
                holder.tv_qianggou.setVisibility(View.GONE);
            }
        });
        try {
            //  不要动
            holder.cb_checked_share_specila_coll.setChecked(isChecked.get(position));
            holder.cb_checked_share_specila_coll.setText(numbers.get(position));
            holder.cb_checked_share_specila_coll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    boolean check;
                    if (checkedNumber < maxChecked) {
                        if (!isChecked.get(position)) {
                            checkedIndex.add(position);
                            checkBox.add(holder.cb_checked_share_specila_coll);
                            checkedNumber++;
                            setTexta();
                        } else {
                            clearText(position, holder.cb_checked_share_specila_coll);
                        }
                        check = !isChecked.get(position);
                    } else {
                        if (checkedNumber == maxChecked && isChecked.get(position)) {
                            clearText(position, holder.cb_checked_share_specila_coll);
                        } else {
                            Toast.makeText(context, "最多只能选" + maxChecked + "个", Toast.LENGTH_SHORT).show();
                        }
                        check = false;
                    }
                    isChecked.set(position, check);
                    holder.cb_checked_share_specila_coll.setChecked(isChecked.get(position));
                }
            });

            Contig.checkedIndexBABY = checkedIndex;

        }catch (Exception e){

        }

        holder.chioce_qianggou_no.setVisibility(View.GONE);
//        holder.tv_lijishare_btn.setVisibility(View.VISIBLE);
        for (int s = 0; s < Contig.ShareListBABY.size(); s++) {
            if (dataBean.getId().equals(Contig.ShareListBABY.get(s))) {
                holder.chioce_qianggou_no.setVisibility(View.VISIBLE);
                holder.tv_qianggou.setVisibility(View.GONE);
                Log.e("5647","youbianhua          "+dataBean.getId());


            }
        }
        holder.ll_mycollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,Red_PacketActivity.class);
                intent.putExtra("shopid",dataBean.getId());
                intent.putExtra("typex","yh");
                context.startActivity(intent);
            }
        });
        //17694830316   123456

    }

    public void clearText(int position, CheckBox cb_checked) {
        for (int i = 0; i < checkedIndex.size(); i++) {
            if (checkedIndex.get(i) == position) {
                numbers.set(position, "");
                checkedIndex.remove(i);
                checkBox.remove(i);
                break;
            }
        }
        checkedNumber--;
        setTexta();
        numbers.set(position, "");
        cb_checked.setText("");
    }

    public void setTexta() {
        for (int i = 0; i < checkedIndex.size(); i++) {
            numbers.set(checkedIndex.get(i), (i + 1) + "");
            checkBox.get(i).setText(numbers.get(checkedIndex.get(i)));

        }
    }


    @Override
    public int getItemCount() {
        return goods==null?0:goods.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {


        ImageView baby_shopimg;
        TextView shopname,price_yj,price_xj,price_count;
        TextView couponAmount,marketProfit;
        TextView tv_qianggou;
        CheckBox cb_checked_share_specila_coll;
        TextView chioce_qianggou_no;
        TextView chioce_qianggoupt;
        LinearLayout ll_mycollect;
        ImageView iv_speclia_jdOryh_coll;

        public MyViewHolder(View itemView) {
            super(itemView);
            baby_shopimg = (ImageView) itemView.findViewById(R.id.itemem_shoim);
            shopname = (TextView) itemView.findViewById(R.id.itemem_shopname);
            price_yj = (TextView) itemView.findViewById(R.id.itemem_yj);
            price_xj = (TextView) itemView.findViewById(R.id.itemem_shopxj);
            price_count = (TextView) itemView.findViewById(R.id.itemem_count);
            couponAmount = (TextView) itemView.findViewById(R.id.itemem_hongbao);
            tv_qianggou = (TextView) itemView.findViewById(R.id.chioce_qianggou);
            cb_checked_share_specila_coll= (CheckBox) itemView.findViewById(R.id.cb_checked_share_specila_coll);
            chioce_qianggou_no= (TextView) itemView.findViewById(R.id.chioce_qianggou_no);
            chioce_qianggoupt= (TextView) itemView.findViewById(R.id.chioce_qianggoupt);
            ll_mycollect= (LinearLayout) itemView.findViewById(R.id.ll_mycollect);
            iv_speclia_jdOryh_coll= (ImageView) itemView.findViewById(R.id.iv_speclia_jdOryh_coll);
            //   marketProfit = (TextView) itemView.findViewById(R.id.itemem_zhuan);
        }
    }

}
